﻿using System;
using System.Collections.Generic;
using System.IO;
using Logger.Object;
using Logger.Util;

namespace Logger.Controller
{
    public static class LoggerController
    {
        #region Write Log
        public static void WriteLog(int _errorCode, Exception _ex, string _note)
        {
            try
            {
                // Get full path of file
                string strFilePath = string.Empty;
                string strFileName = string.Empty;
                string strFullLogPath = GetPath(ref strFilePath, ref strFileName);
                if (string.IsNullOrEmpty(strFullLogPath))
                    return;

                // Get log list from file
                List<LogCode> lcList = ConvertLog.GetLogListFromText(Files.ReadFile(strFilePath, strFileName));

                // Search Log Code item in list
                // If there is no item, create new one then add it
                LogCode lcItem = lcList.Find(item => item.ErrorCode == _errorCode);
                if (lcItem == null)
                {
                    lcItem = new LogCode(_errorCode);
                    lcList.Add(lcItem);
                }
                lcItem.ErrorCount++;

                // Create new LogDetail
                LogDetail ldItem = new LogDetail();
                ldItem.Note = _note;

                // Add new description
                lcItem.ErrorDetail.Add(ldItem);

                // Check if there is exception
                if (_ex != null)
                {
                    // Set Message for LogCode
                    lcItem.Message = _ex.Message.Replace("\r\n", "").Replace("\t", "");

                    // Set description for LogDetail
                    ldItem.Description = _ex.StackTrace;

                    //try
                    //{
                    //    StackTrace stackTrace = new StackTrace();
                    //    string MethodName = stackTrace.GetFrame(1).GetMethod().Name;
                    //    string Content = string.Concat(DateTime.Now.ToString(), "----Object[Method] : ", MethodName, "-----Message : ", exp.Message, "-----Source : ", exp.StackTrace, Environment.NewLine);
                    //    File.AppendAllText(FullLogPath, Content, System.Text.Encoding.UTF8);
                    //}
                    //catch
                    //{
                    //    return;
                    //}
                }

                // Write log file
                Files.WriteFile(ConvertLog.GetTextFromLogList(lcList), strFilePath, strFileName);
            }
            catch { }
        }

        public static void WriteLog(int _errorCode)
        {
            WriteLog(_errorCode, null, string.Empty);
        }

        public static void WriteLog(int _errorCode, Exception _ex)
        {
            WriteLog(_errorCode, _ex, string.Empty);
        }

        public static void WriteLog(int _errorCode, string _note)
        {
            WriteLog(_errorCode, null, _note);
        }

        public static void WriteLog(string _note)
        {
            WriteLog(Constants.C_GENERAL_ERRORCODE, null, _note);
        }

        public static void WriteLog(Exception _ex)
        {
            WriteLog(Constants.C_GENERAL_ERRORCODE, _ex, string.Empty);
        }

        public static void WriteLog(Exception _ex, string _note)
        {
            WriteLog(Constants.C_GENERAL_ERRORCODE, _ex, _note);
        }
        #endregion

        #region Read Log
        public static List<LogDetail> ReadLog(string filePath)
        {
            var lst = new List<LogDetail>();
            try
            {
                // Get log list from file
                List<LogCode> lcList = ConvertLog.GetLogListFromText(Files.ReadFile(filePath));
                lcList.ForEach(x => lst.AddRange(x.ErrorDetail));
            }
            catch { }
            return lst;
        }
        #endregion

        #region PrivateFunctions
        private static string GetPath(ref string _filePath, ref string _fileName)
        {
            string FullLogPath = string.Empty;
            try
            {
                string ErrorDate = string.Concat(DateTime.Now.ToString("yyyy-MM"));
                _filePath = AppDomain.CurrentDomain.BaseDirectory;
                _filePath = Path.Combine(_filePath, "Log");
                _filePath = Path.Combine(_filePath, ErrorDate);
                if (!Directory.Exists(_filePath))
                {
                    Directory.CreateDirectory(_filePath);
                }
                _fileName = string.Concat(DateTime.Now.ToString("yyyy-MM-dd"), ".txt");
                FullLogPath = Path.Combine(_filePath, _fileName);
            }
            catch { }
            return FullLogPath;
        }
        #endregion
    }
}
